#include<iostream>
#include<string.h>

using namespace std;

class String
{
public:
    String(const char* p=nullptr)
    {
        if(p!=nullptr)
        {
            _pstr = new char[strlen(p) + 1];
            strcpy(_pstr, p);
        }
        else
        {
            _pstr = new char[1];
            *_pstr = '\0';
        }
    }
    ~String()
    {
        delete[] _pstr;
        _pstr = nullptr;
    }
    String(const String& src)
    {
        _pstr = new char[strlen(src._pstr) + 1];
        strcpy(_pstr, src._pstr);
    }
    String& operator=(const String& src)
    {
        if(this==&src)
            return *this;
        delete[] _pstr;
        _pstr = new char[strlen(src._pstr) + 1];
        strcpy(_pstr, src._pstr);
        return *this;
    }
    bool operator>(const String &src) const
    {
        return strcmp(_pstr, src._pstr) > 0;
    }
    bool operator==(const String &src) const
    {
        return strcmp(_pstr, src._pstr) == 0;
    }
    bool operator<(const String &src) const
    {
        return strcmp(_pstr, src._pstr) < 0;
    }
    int length()const
    {
        return strlen(_pstr);
    }
    char &operator[](int index) { return _pstr[index]; }
    const char &operator[](int index) const { return _pstr[index]; }
    const char *c_str() const { return _pstr; }
    
    class iterator
    {
    public:
        iterator(char *p = nullptr) : _p(p){}
        void operator++()
        {
            ++_p;
        }
        bool operator!=(const iterator& src)
        {
            if(_p!=src._p)
                return true;
            return false;
        }
        char& operator*()
        {
            return *_p;
        }

    private:
        char* _p;
    };
    iterator begin() { return iterator(_pstr); };
    iterator end() { return iterator(_pstr + length()); }

private:
    char *_pstr;
    friend String operator+(const String &lsrc, const String &rsrc);
    friend ostream &operator<<(ostream &out, const String &src);
    friend istream &operator>>(istream &in, String &src);
};

String operator+(const String &lsrc,const String &rsrc)
{
    // char *temp = new char[strlen(lsrc._pstr) + strlen(rsrc._pstr) + 1];
    // strcpy(temp, lsrc._pstr);
    // strcat(temp, rsrc._pstr);
    // String s(temp);
    // delete[] temp;
    String s;
    s._pstr = new char[strlen(lsrc._pstr) + strlen(rsrc._pstr) + 1];
    strcpy(s._pstr, lsrc._pstr);
    strcat(s._pstr, rsrc._pstr);
    return s;
}
ostream& operator<<(ostream& out,const String& src)
{
    out << src._pstr;
    return out;
}
istream &operator>>(istream &in, String &src)
{
    in >> src._pstr;
    return in;
}

int main()
{
    String str1;
    String str2 = "aaa";
    String str3 = "bbb";
    String str4 = str2 + str3;
    String str5 = str2 + "ccc";
    String str6 = "ddd" + str2;

    cout << "str6:" << str6 << endl;
    if(str5>str6)
    {
        cout << str5 << ">" << str6 << endl;
    }
    else
    {
        cout << str5 << "<" << str6 << endl;
    }
    int len = str6.length();
    for (int i = 0; i < len;i++)
    {
        cout << str6[i] << " ";
    }
    cout << endl;

    char buf[1024] = {0};
    strcpy(buf, str6.c_str());
    cout << "buf:" << buf << endl;

    String a = "ababas";
    // 迭代器的作用：提供统一的形式，来遍历容器
    String::iterator it = a.begin();
    for (; it != a.end();++it)
    {
        cout << *it << " ";
    }

    return 0;
}